package dao;

import java.util.List;
import java.util.Map;

import entity.Book;

@SuppressWarnings("unchecked")
public class BookDao extends DangDao{

	protected String getNameSpace() {
		return Book.class.getSimpleName().toLowerCase();
	}
	
	public List<Map<String, Object>> queryRecommendBooks() throws Exception{
		return queryBySql("queryRecommendBooks", new Object[]{});
	}
	
	public List<Map<String, Object>> queryHotBooks() throws Exception{
		return queryBySql("queryHotBooks", new Object[]{});
	}
	
	public List<Map<String, Object>> queryNewBooks() throws Exception{
		return queryBySql("queryNewBooks", new Object[]{});
	}
	
	public List<Map<String, Object>> queryBookByCid(Map<String, Object> map) throws Exception{
		List<Map<String, Object>> returnList = null;
		
		String order= (String) map.get("order");
		if (order.equals("add_time")) {
			returnList = (List<Map<String, Object>>) queryForList("queryBookByCidByAddTime", map);
		} else if (order.equals("dang_price")) {
			returnList = (List<Map<String, Object>>) queryForList("queryBookByCidByDangPrice", map);
		}
		return returnList;
	}
	
	public Long queryTotalSizeByCid(Integer cid) throws Exception{
		return (Long) queryForObject("queryTotalSizeByCid", cid);
	}
	
	public Book queryBookById(Integer id) throws Exception{
		List<Map<String, Object>> bookList = (List<Map<String, Object>>) queryForList("queryBookById", id);
		if (bookList != null && bookList.size() > 0) {
			Map<String, Object> map = bookList.get(0);
			Book book = new Book();
			book.setId((Integer)map.get("id"));
			book.setProduct_name((String)map.get("product_name"));
			book.setDescription((String)map.get("description"));
			book.setAdd_time((Long)map.get("add_time"));
			book.setFixed_price((Double)map.get("fixed_price"));
			book.setDang_price((Double)map.get("dang_price"));
			book.setKeywords((String)map.get("keywords"));
			book.setHas_deleted((Integer)map.get("has_deleted"));
			book.setProduct_pic((String)map.get("product_pic"));
			book.setAuthor((String)map.get("author"));
			book.setPublishing((String)map.get("publishing"));
			book.setPublish_time((Long)map.get("publish_time"));
			book.setWord_number((String)map.get("word_number"));
			book.setWhich_edtion((String)map.get("which_edtion"));
			book.setTotal_page((String)map.get("total_page"));
			book.setPrint_time((Integer)map.get("print_time"));
			book.setPrint_number((String)map.get("print_number"));
			book.setIsbn((String)map.get("isbn"));
			book.setAuthor_summary((String)map.get("author_summary"));
			book.setCatalogue((String)map.get("catalogue"));
			return book;
		}
		return null;
	}
}
